//
//  WFPasswordModifyViewController.m
//  WisdomFarm
//
//  Created by 心情 on 2017/10/26.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFPasswordModifyViewController.h"
#import "WFPasswordModifyCell.h"
#import "WFButtonTableFooterView.h"
#import "WFRegisterRequest.h"

@interface WFPasswordModifyViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) WFPasswordModifyCell *oldCell;
@property (nonatomic, strong) WFPasswordModifyCell *modifyCell;
@property (nonatomic, strong) WFPasswordModifyCell *confirmCell;
@property (nonatomic, strong) WFButtonTableFooterView *footerView;


@end

@implementation WFPasswordModifyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.footerView = [WFButtonTableFooterView new];
    [self.view addSubview:self.footerView];
    
    self.tableView.allowsSelection = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    
    [self.footerView.saveButton setTitle:@"确认提交" forState:UIControlStateNormal];
    [self.footerView.saveButton addTarget:self action:@selector(onSave) forControlEvents:UIControlEventTouchUpInside];

    
    self.oldCell = [WFPasswordModifyCell new];
    self.oldCell.titleLabel.text = @"原密码";
    self.oldCell.passwordField.placeholder = @"请输入原密码";

    self.modifyCell = [WFPasswordModifyCell new];
    self.modifyCell.titleLabel.text = @"新密码";
    self.modifyCell.passwordField.placeholder = @"请输入新密码";
    
    self.confirmCell = [WFPasswordModifyCell new];
    self.confirmCell.titleLabel.text = @"确认密码";
    self.confirmCell.passwordField.placeholder = @"请再次输入新密码";

    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.insets(UIEdgeInsetsZero);
        make.bottom.equalTo(self.footerView.mas_top);
    }];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50);
    }];
}

- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    [self.footerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50 + self.view.safeAreaInsets.bottom);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return self.oldCell;
            break;
        case 1:
            return self.modifyCell;
        default:
            return self.confirmCell;
            break;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (void)onSave {
    NSString *oldPassword = self.oldCell.passwordField.text;
    NSString *modifyPassword = self.modifyCell.passwordField.text;
    NSString *confirmPassword = self.confirmCell.passwordField.text;
    if (oldPassword.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入原密码"];
        return;
    }
    if (modifyPassword.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入新密码"];
        return;
    }
    if (confirmPassword.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入确认密码"];
        return;
    }
    if (![confirmPassword isEqualToString:modifyPassword]) {
        [SVProgressHUD showInfoWithStatus:@"两次密码不一致"];
        return;
    }
    //修改密码
    WFPasswordModifyRequest *request = [WFPasswordModifyRequest new];
    request.id = [WFStore currentUser].id;
    request.phone = [WFStore currentUser].phone;
    request.oldpassword = [oldPassword MD5];
    request.newpassword = [modifyPassword MD5];
    [request startWithBlock:^(__kindof MZBaseRequest *request, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            return;
        }
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
